#include <stdio.h>
#include <stdlib.h>
#include <pic18f6722.h>

#include "Logic_PowerStapler.h"
#define MOTOR_VOLTAGE_STOP 300
#define MOTOR_VOLTAGE_WARN 330
//*********************************************************************
/* Initialize phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_Initialize()
{
 }

//*********************************************************************
/* Standby phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_Standby()
{
    Device_MotorOutControl(0);


}

//*********************************************************************
/* ElasticClosure phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_ElasticClosure() 
{
//    Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticClosureCurrent);
    Device_MotorSetCurrent(0xff);
    Device_MotorOutControl(FORWARD);
    if(Charge_Status == 0)
    {
        if(warn_flag==0)
        {
           // Device_Indicator_SetLED(0);
            Device_Indicator_SetLED(2);  //green   
        }
      
    }    
   int flag=0;
    while (1) 
    {
      unsigned int  Battery1_Voltage = Device_LTC2943_ReadVoltage();
      if((Battery1_Voltage<=MOTOR_VOLTAGE_WARN)&&(Battery1_Voltage>=MOTOR_VOLTAGE_STOP))
      {
          if(flag!=1)
          {
             Device_Indicator_SetLED(0);
             work_led_flag=1;
          }
          else
            Device_Indicator_SetLED(3);    //yellow 
      }
      else
      {
          work_led_flag=0;
          if(flag!=1)
           Device_Indicator_SetLED(2);  //green 
      }
      if(Battery1_Voltage<MOTOR_VOLTAGE_STOP)
      {
          Device_Indicator_SetLED(1);    //red 
          work_led_flag=2;
          Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticClosureCurrent);
          Device_MotorOutControl(0);
          flag=1;
      }
        if (Device_Trigger_CheckFire() == SWITCH_OFF) 
        {
              work_led_flag=0;
              Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticClosureCurrent);
              Device_MotorOutControl(0);
//              set_yellow_led(0);
             break;
        }
           
    }

}

//*********************************************************************
/* ElasticOpen phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_ElasticOpen()
{
//    Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticClosureCurrent);
    Device_MotorSetCurrent(0x0ff);
    Device_MotorOutControl(BACK);
    if(Charge_Status == 0)
    {
        if(warn_flag==0)
        {
            // Device_Indicator_SetLED(0);  //green 
             Device_Indicator_SetLED(2);  //green 
        }
      
    }
    int flag=0;
    while (1) 
    {
//        if (Device_Trigger_CheckSafe() == SWITCH_OFF) 
//            break;
        unsigned int  Battery1_Voltage = Device_LTC2943_ReadVoltage();
        if((Battery1_Voltage<=MOTOR_VOLTAGE_WARN)&&(Battery1_Voltage>=MOTOR_VOLTAGE_STOP))
        {
//            Device_Indicator_SetLED(0);
//            work_led_flag=1;
           // Device_Indicator_SetLED(3);    //yellow 
             if(flag!=1)
          {
             Device_Indicator_SetLED(0);
             work_led_flag=1;
          }
           else
             Device_Indicator_SetLED(3);      
        }else
        {
            work_led_flag=0;
            if(flag!=1)
            Device_Indicator_SetLED(2);  //green 
        }
        if(Battery1_Voltage<MOTOR_VOLTAGE_STOP)
        {
            Device_Indicator_SetLED(1);    //red 
            work_led_flag=2;
            Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticClosureCurrent);
            Device_MotorOutControl(0);
            flag=1;
            
        }
        if (Device_Trigger_CheckSafe() == SWITCH_OFF) 
        {
            work_led_flag=0;
            Device_MotorSetCurrent(PowerStaplerData.MotorControl.ElasticClosureCurrent);
            Device_MotorOutControl(0);
//            set_yellow_led(0);
             break;
        }
    }

}






























//*********************************************************************
/* cut read phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-26
*/
//**********************************************************************
void Phase_CutSafty()
{
    uint ElasticCutSaftyValue;
    Device_Indicator_SetDirectorLED(1,0);
    
    ElasticCutSaftyValue = Logic_MotorDriver_Phase_Safty();
    
    switch(ElasticCutSaftyValue)
    {
        case 0:    ////Trigger B is close
            PowerStaplerData.Phase_state = phase_CUTREADY;
            break;
        case 1:   //SaftyToOpen
            PowerStaplerData.Phase_state = Phase_ELASTICOPEN;
            break;
        case 2:   //error
            PowerStaplerData.Phase_state = Phase_ERROR;
            PowerStaplerData.ErrorCode = ELASTIC_OPEN_OVER_TIME;
            break;
        default:
            break;
    }
   
}

//*********************************************************************
/* from cutSafty back to Open
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-27
*/
//**********************************************************************
void Phase_SaftyToOpen()
{
    uint SaftyToOpenValue;
    
    SaftyToOpenValue = Logic_MotorDriver_SaftyToOpenValue();
    
    switch(SaftyToOpenValue)
    {
        case 0:    ////Trigger B is close
            PowerStaplerData.Phase_state = Phase_STANDBY;
            break;
        case 2:   //error
            PowerStaplerData.Phase_state = Phase_ERROR;
            PowerStaplerData.ErrorCode = ELASTIC_OPEN_OVER_TIME;
            break;
        default:
            break;
    }
}

//*********************************************************************
/* cut ready; check Trigger c
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_CutReady()
{
    uint ElasticCutReadyValue;
    
    ElasticCutReadyValue = Logic_MotorDriver_CutReady();
    
    switch(ElasticCutReadyValue)
    {
        case 0:    ////Trigger B is close
            PowerStaplerData.Phase_state = Phase_CUTTING;
            break;
        case 1:   //SaftyToOpen
            PowerStaplerData.Phase_state = phase_SAFTYTOOPEN;
            break;
        case 2:   //error
            PowerStaplerData.Phase_state = Phase_ERROR;
            PowerStaplerData.ErrorCode = ELASTIC_OPEN_OVER_TIME;
            break;
        default:
            break;
    }
}
//*********************************************************************
/* cutting phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_Cutting()
{
    uint CuttingValue;
    
    PowerStaplerData.Indicator.ForwardLed_Flash = 1;

    
    CuttingValue = Logic_MotorDriver_Cutting();
    switch(CuttingValue)
    {
       case 0:
           PowerStaplerData.Phase_state = phase_CUTEND;
           break;
       case 1:
           PowerStaplerData.Phase_state = phase_CUTPAUSE;
           break;
       case 2:
           PowerStaplerData.Phase_state = Phase_ERROR;
           PowerStaplerData.ErrorCode = ELASTIC_OPEN_OVER_TIME;
           break;
       default:
           break;
    } 
}

//*********************************************************************
/* Cut pause
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_CutPause()
{
    uint CutPauseValue;
    
    
    CutPauseValue = Logic_MotorDriver_CutPause();
    switch(CutPauseValue)
    {
       case 0:
           PowerStaplerData.Phase_state = Phase_CUTTING;
           break;
       case 1:
           PowerStaplerData.Phase_state = Phase_RETURN;
           break;
       case 2:
           PowerStaplerData.Phase_state = Phase_ERROR;
           PowerStaplerData.ErrorCode = ELASTIC_OPEN_OVER_TIME;
           break;
       default:
           break;
    } 
    
}

//*********************************************************************
/* cut end
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-28
*/
//**********************************************************************
void Phase_CutEnd()
{
    uint CutEndValue;
     
  //  PowerStaplerData.Indicator.ForwardLed_Flash = 0;
 //   PowerStaplerData.Indicator.BackLED_Flash = 0;
 //   Device_Indicator_SetDirectorLED(3,0);
    
    CutEndValue = Logic_MotorDriver_CutEnd();
    switch(CutEndValue)
    {
       case 0:
           PowerStaplerData.Phase_state = Phase_RETURN;
           break;
       case 1:
           PowerStaplerData.Phase_state = phase_CUTEND;
           break;
       case 2:
           PowerStaplerData.Phase_state = Phase_ERROR;
           PowerStaplerData.ErrorCode = ELASTIC_OPEN_OVER_TIME;
           break;
       default:
           break;
    } 

     
}
//*********************************************************************
/* return phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_Return()
{
  uint ReturnValue;
  
  PowerStaplerData.Indicator.ForwardLed_Flash = 0;
  PowerStaplerData.Indicator.BackLED_Flash = 1;
  
  ReturnValue = Logic_MotorDriver_Return();
  
  switch(ReturnValue)
    {
       case 0:
           PowerStaplerData.Phase_state = Phase_STANDBY;
           break;
       case 1:
           PowerStaplerData.Phase_state = phase_RETRUNPAUSE;
           break;
       case 2:
           PowerStaplerData.Phase_state = Phase_ERROR;
           PowerStaplerData.ErrorCode = ELASTIC_OPEN_OVER_TIME;
           break;
       default:
           break;
    }   
}

//*********************************************************************
/* error process phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_ReturnPause()
{
    uint ReturnPauseValue;

    ReturnPauseValue = Logic_MotorDriver_ReturnPause();
    switch (ReturnPauseValue) 
    {
        case 0:
            PowerStaplerData.Phase_state = Phase_RETURN;
            break;
        case 1:
            PowerStaplerData.Phase_state = Phase_CUTTING;
            break;
        case 2:
            PowerStaplerData.Phase_state = Phase_ERROR;
            PowerStaplerData.ErrorCode = ELASTIC_OPEN_OVER_TIME;
            break;
        default:
            break;
    }
    
}
//*********************************************************************
/* error process phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Phase_Error()
{
    Logic_ErrorProcess();  
}

//*********************************************************************
/* Initialize phase
 * input void 
 * return: void
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
void Logic_Initialize_SetDataDefault()
{
      PowerStaplerData.Phase_state = Phase_INILIZATION;
      PowerStaplerData.MotorControl.FeedbackStatus = 0;
      PowerStaplerData.MotorControl.ElasticClosureCurrent =  0x0a;    //24%
      PowerStaplerData.MotorControl.ElasticOpenCurrent = 0x0a;       //47%
      PowerStaplerData.MotorControl.CuttingCurrent = 0x1b;
      PowerStaplerData.MotorControl.ReturnCurrent = 0x1b;
      PowerStaplerData.Indicator.ForwardLed_Flash = 0;
}

//*********************************************************************
/* check battery install
 * input void 
 * return: 0- install is ok, NORMAL_WORK 1- POWER_MANAGE_BATTERY_NO_INSTALL
 * author: Guichuan.He
 * date: 2016-10-09
*/
//**********************************************************************
uint Logic_PowerStapler_CheckAdapter()
{

}